|
 |
Daniel Bastos <dbastos+0### [at] toledo com> wrote:
> #!/usr/bin/env mzscheme
> (module cat scheme/base
>
> (define (interact f)
> (let ([ln (read-line)])
> (cond
> [(not (eq? ln eof))
> (and
> (printf "~a\n" (f ln))
> (interact f))])))
>
> (define (id x) x)
>
> (define (cat files)
> (for-each
> (lambda (x)
> (with-input-from-file x
> (lambda ()
> (interact id))))
> files))
>
> ;main
> (cat (vector->list (current-command-line-arguments)))
> )
>
> With with-input-from-file, we get the stdin connected to the file
> opened, so no changes in interact were required.
That's the PLT way. here's another:
#!r6rs
(import (rnrs base)(rnrs io simple)(rnrs programs))
; simple and straightforward standard R6RS scheme solution
(define (cat files)
(for-each
(lambda (file)
(call-with-input-file file
(lambda (port)
; no simple line-oriented IO in standard R6RS
; so you either write one or loop char-by-char
(let go ((c (read-char port)))
(if (not (eof-object? c))
(begin
(write-char c)
(go (read-char port))))))))
files))
(cat (cdr (command-line)))
you can run it like:
plt-r6rs cat.scm foo.txt bar.txt
> This cat does not handle exceptions. I might see how that works next.
Neither does mine. There is a file-exists? function though:
http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-10.html#node_chap_9
> Lisp likes might be hackestables.
I just love Lisp, Scheme in particular. Minimalist like C, only much higher
level. Incidentally, the only two languages I truly enjoy, for different
purposes sure.
yes, I'm aware the C solution for this particular example would be way
shorter... :P
Post a reply to this message
|
 |